package com.llf.Controller;


import com.llf.Pojo.Content;
import com.llf.Pojo.Department;
import com.llf.Pojo.Employee;
import com.llf.Service.ContentServiceImpl;
import com.llf.Service.DepartmentServiceImpl;
import com.llf.Service.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Collection;

@Controller
public class ControllerEmp {
    @Autowired
    private DepartmentServiceImpl departmentService;
    @Autowired
    private EmployeeServiceImpl employeeService;
    @Autowired
    private ContentServiceImpl contentService;
    @RequestMapping("/emps")
    public  String getall(Model model){
        ArrayList<Employee> employees = employeeService.getall();
        model.addAttribute("emps",employees);
        return "list";

    }
    @GetMapping("/emp")
    public String toadd(Model model){
        ArrayList<Department> departments = departmentService.getall();
        model.addAttribute("dept",departments);
        return "addemp";
    }
    @PostMapping("/emp")
    public String add(Employee employee){
        employeeService.addEmp(employee);
        return "redirect:/emps";

    }
    @GetMapping("/toupdate/{id}")
    public  String toupdate(@PathVariable("id") Integer id,Model model){
        Employee employee = employeeService.getByid(id);
        model.addAttribute("emp",employee);
        ArrayList<Department> departments = departmentService.getall();
        model.addAttribute("dept",departments);
        return "update";
    }
    @PostMapping("/update")
    public String update(Employee employee){
        employeeService.updateById(employee);
        return "redirect:/emps";
    }
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        employeeService.deleteById(id);
        return "redirect:/emps";

    }
    @GetMapping("/toproduct")
    public String toproduct(){
        return "product";
    }
    @GetMapping("/tomain")
    public String tomain(){
        return "redirect:/main";
    }

    @PostMapping("/tosuccess")
    public String tosuccess( String username,Model model){
        model.addAttribute("msg",username);
        return "success";

    }
    @GetMapping("/toadvice")
    public String toadvice(Model model){
        ArrayList<Content> contents = contentService.getall();
        model.addAttribute("contents",contents);
        return "advice";
    }


}

